/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include <devmon/driver_abi.h>
#include <mc/log.h>

#include "average.h"

using namespace dev;

// 导出信息实现
extern "C" {

driver_handle_t create_average(void* service, const char* name) {
    try {
        if (service == nullptr) {
            elog("create average failed: service is null");
            return nullptr;
        }
        if (name == nullptr) {
            elog("create average failed: name is null");
            return nullptr;
        }
        Average* device = new Average();
        device->set_service(static_cast<mc::engine::service*>(service));
        device->set_object_name(name);
        return (void*)device;
    } catch (const std::exception& e) {
        elog("Error creating average: ${error}", ("error", e.what()));
        return nullptr;
    }
}

status_t init_average(driver_handle_t device, void* csr_object, void*) {
    if (device == nullptr) {
        elog("init average failed: device is null");
        return STATUS_ERROR;
    }
    if (csr_object == nullptr) {
        elog("init average failed: csr_object is null");
        return STATUS_ERROR;
    }
    
    Average*          device_object  = static_cast<Average*>(device);
    mc::mutable_dict* csr_object_ptr = static_cast<mc::mutable_dict*>(csr_object);
    bool              ret            = device_object->init(*csr_object_ptr);
    if (!ret) {
        return STATUS_ERROR;
    }
    return STATUS_OK;
}

status_t start_average(driver_handle_t device) {
    return STATUS_OK;
}

status_t stop_average(driver_handle_t device) {
    return STATUS_OK;
}

device_driver_t average_device_driver = {.device_name = "Average",
                                         .ctor        = create_average,
                                         .init        = init_average,
                                         .start       = start_average,
                                         .stop        = stop_average};

device_driver_t average_device_driver_list[] = {average_device_driver};
status_t        register_device_driver(device_driver_t** device_driver, uint8_t* count) {
    *device_driver = average_device_driver_list;
    *count = sizeof(average_device_driver_list) / sizeof(average_device_driver_list[0]);
    return STATUS_OK;
}

} // extern "C"
